package com.gome.wechat.program.service.applicationservice.impl;

import com.gome.wechat.program.common.consts.GroupActivityUserType;
import com.gome.wechat.program.common.util.DateUtil;
import com.gome.wechat.program.common.util.MyBeanUtil;
import com.gome.wechat.program.dto.GroupActivityDto;
import com.gome.wechat.program.dto.GroupActivityJoinDto;
import com.gome.wechat.program.entity.GroupActivity;
import com.gome.wechat.program.entity.GroupActivityUser;
import com.gome.wechat.program.entity.GroupPicture;
import com.gome.wechat.program.service.GroupActivityService;
import com.gome.wechat.program.service.GroupActivityUserService;
import com.gome.wechat.program.service.GroupPictureService;
import com.gome.wechat.program.service.applicationservice.GroupActivityApplicationService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * Created by liwei113 on 2017/11/2 0002.
 */
@Service
public class GroupActivityApplicationServiceImpl implements GroupActivityApplicationService {

    @Autowired
    GroupActivityService groupActivityService;
    @Autowired
    GroupActivityUserService groupActivityUserService;
    @Autowired
    GroupPictureService groupPictureService;



    @Override
    @Transactional
    public Long joinGroupActivity(GroupActivityJoinDto groupActivityJoinDto) {
        GroupActivityUser groupActivityUser = GroupActivityUser.create(groupActivityJoinDto.getGroupActivityId(),
                groupActivityJoinDto.getUserId(),GroupActivityUserType.JOINER.value());
        groupActivityUser =  groupActivityUserService.save(groupActivityUser);
        return groupActivityUser.getId();
    }

    @Override
    @Transactional
    public GroupActivityDto save(GroupActivityDto groupActivityDto) {
        Long id = groupActivityDto.getId();
        GroupActivity groupActivity = new GroupActivity();
        MyBeanUtil.copyProperties(groupActivityDto,groupActivity);
        String appointmentEndTimeStr =  groupActivityDto.getAppointmentEndTimeStr();
        String appointmentStartTimeStr =  groupActivityDto.getAppointmentStartTimeStr();
        groupActivity.setAppointmentStartTime(DateUtil.parseYMDHMS(appointmentStartTimeStr));
        groupActivity.setAppointmentEndTime(DateUtil.parseYMDHMS(appointmentEndTimeStr));
        groupActivity = groupActivityService.save(groupActivity);
        if(id==null){
            GroupActivityUser groupActivityUser = GroupActivityUser.create(groupActivity.getId(),groupActivity.getPublisherId(),
                    GroupActivityUserType.PUBLISHER.value());
            groupActivityUserService.save(groupActivityUser);
            groupPictureService.deleteByGroupActivityId(id);
        }
        List<String> pics = groupActivityDto.getPics();
        if(!CollectionUtils.isEmpty(pics)){
            List<GroupPicture> list = new ArrayList<>();
            for(String pic:pics){
                list.add(new GroupPicture(groupActivity.getId(),pic));
            }
            groupPictureService.saveList(list);
        }

        groupActivityDto.setId(groupActivity.getId());
        return groupActivityDto;
    }
    
    
    public List<GroupActivity> getListByUserId(Long userId){
    	List<GroupActivityUser> userList = groupActivityUserService.getListByUserId(userId);
//    	String activityIds ="(";
//    	for (GroupActivityUser groupActivityUser : userList) {
//    		activityIds+=groupActivityUser.getGroupActivityId()+",";
//		}
//    	if(activityIds.equals("(")){
//    		return new ArrayList<>();
//    	}
//
//    	activityIds=activityIds.substring(0, activityIds.length()-1)+")";


        List<Long> activityIds = new ArrayList<>();
        for (GroupActivityUser groupActivityUser : userList) {
            activityIds.add(groupActivityUser.getGroupActivityId());
		}
        List<GroupActivity> result = null;
        if(!CollectionUtils.isEmpty(activityIds)){
            result =  groupActivityService.getListInActivityIds(activityIds);
        }
        if(CollectionUtils.isEmpty(result)){
            result = new ArrayList<>();
        }
    	return result;
    }

	@Override
	public List<GroupActivity> getGroupActivityByPublisherId(Long publisherId) {
		return groupActivityService.getGroupActivityByPublisherId(publisherId);
	}

	@Override
	public List<GroupActivity> getGroupActivityByCoordinate(Double longitude, Double latitude, Double space) {

        Double longitudeLeft = longitude- space;
        Double longitudeRight = longitude+space;
        Double latitudeUp = latitude-space;
        Double latitudeDown = latitude+space;
		
		return groupActivityService.getGroupActivityByCoordinate(longitudeLeft, longitudeRight, latitudeUp, latitudeDown);
	}

    public List<GroupActivity> getGroupActivityByTypeTwoAndLimitOne(){
        List<GroupActivity> temp =  groupActivityService.getGroupActivityByTypeTwoAndLimitOne();

        List<GroupActivity> result= new ArrayList<>();
        if(temp.size()==0){
            return new ArrayList<>();
        }

        int i = (int)Math.random()*temp.size();
        result.add(temp.get(i));
        return  result;
    }

    @Override
    public boolean checkJoin(GroupActivityJoinDto groupActivityJoinDto) {
        GroupActivityUser groupActivityUser = groupActivityUserService.findByUserAndActivityId(groupActivityJoinDto.getUserId(),
                groupActivityJoinDto.getGroupActivityId());
        if(groupActivityUser==null){
            return false;
        }
        return true;
    }
}
